package com.sys.console.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.sys.console.business.SysAuthorityService;
import com.sys.console.business.SysModuleAuthorityService;
import com.sys.console.business.SysModuleService;
import com.sys.console.mapper.Sys_Module_AuthorityMapper;
import com.sys.console.mapper.Sys_Role_AuthorityMapper;
import com.sys.console.pojo.MeunAuthorityTreeJsonDto;
import com.sys.console.pojo.SysModuleDto;
import com.sys.console.pojo.SysRoleModuleDto;
import com.sys.console.pojo.Sys_Module;
import com.sys.object.common.ObjException;
import com.sys.object.common.ObjResultCode;
import com.sys.object.common.ObjResultException;
import com.sys.object.common.ObjResultJSON;

/**
 * 2014-11-06
 * 
 * @author 陈成军 模块管理 action
 */
@Controller
public class SysModuleAction {

	@Resource SysModuleService sysModuleService;
	@Resource SysModuleAuthorityService sysModuleAuthorityService;
	@Resource SysAuthorityService sysAuthorityService;
	
	@Resource Sys_Role_AuthorityMapper sysRoleAuthorityMapper; 
	@Resource Sys_Module_AuthorityMapper sysModuleAuthorityMapper;
	
	
	@RequestMapping("/sysModuleTreeDto.do")
	@ResponseBody
	public List<SysModuleDto> sysModuleTreeDtoJSON(String id) {
		Sys_Module module = new Sys_Module();
		module.setEnable(null);
		if (StringUtils.hasText(id)) {
			module.setPid(Integer.parseInt(id));
		}
		try {
			List<SysModuleDto> list = sysModuleService.selectByTree(module);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ArrayList<SysModuleDto>();
	}

	@RequestMapping("/sysModuleList.do")
	public String systemFrameset(Sys_Module sys_module, Model model) {

		return "/system/module/ModuleFrameset";
	}

	@RequestMapping("/sysModuleTree.do")
	public String menuTree(Sys_Module sys_module, Model model) {

		return "/system/module/ModuleTree";
	}

	@RequestMapping("/sysModule.do")
	public String sysModule(Sys_Module module, Model model,Integer refreshTree) {
		try {
			//初始化模块信息
			Sys_Module sys_module = sysModuleService.selectByPrimaryKey(module.getMid());
			if(sys_module!=null&&sys_module.getMid()!=null){
				//初始化模块拥有的权限
				model.addAttribute("refreshTree", refreshTree);
				model.addAttribute("entity", sys_module);
			}else{
				model.addAttribute("entity", module);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
			
		return "/system/module/add";
	}
	
	@RequestMapping("/sysModuleByLayer.do")
	public String sysModuleByLayer(Sys_Module module, Model model) {
		try {
			//初始化模块信息
			Sys_Module sys_module = sysModuleService.selectByPrimaryKey(module.getMid());
			if(sys_module!=null&&sys_module.getMid()!=null){
				//初始化模块拥有的权限
				String sys_moduleauthority = sysAuthorityService.selectByMid(module.getMid());
				model.addAttribute("entity", sys_module);
			}else{
				model.addAttribute("entity", module);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
			
		return "/system/module/addLayer";
	}

	/**
	 * 新增模块 POST
	 */
	@RequestMapping(value = "/saveSysModule.do", method = RequestMethod.POST)
	public String addSysModule(
			@Validated @ModelAttribute("module") Sys_Module module,
			BindingResult result, Model model, String authority) {
		if (result.hasErrors()) {
			return "/system/module/add";
		}
		ObjResultException<String> ret = new ObjResultException<String>();
		try {
			int success =  sysModuleService.saveOrUpdate(module); //修改模块信息
			if (success > 0) {
				ret.setResultCode(ObjResultCode.OK);
			} else {
				ret.setResultCode(ObjResultCode.MODULE_ADD_ERROR);
			}
		} catch (Exception e) {

			if (e.getClass().equals(ObjException.class)) {
				ret.setResultCode(((ObjException) e).getCode());
			} else {
				ret.setResultCode(ObjResultCode.ERROR);
			}
		}
		return "redirect:/sysModule.do?mid=" + module.getMid()+"&refreshTree=1";
	}
	
	/**
	 * 新增模块 POST
	 */
	@RequestMapping(value = "/saveSysModuleJson.do", method = RequestMethod.POST)
	@ResponseBody
	public ObjResultJSON<Sys_Module> saveSysModuleJson(@RequestBody Sys_Module module, Model model) {

		ObjResultJSON<Sys_Module> ret = new ObjResultJSON<Sys_Module>();
		try {
			int success =  sysModuleService.saveOrUpdate(module); //修改模块信息
			if (success > 0) {
				ret.setResultCode(ObjResultCode.OK);
				ret.setData(module);
			} else {
				ret.setResultCode(ObjResultCode.MODULE_ADD_ERROR);
			}
		} catch (Exception e) {
			e.printStackTrace();
			if (e.getClass().equals(ObjException.class)) {
				ret.setResultCode(((ObjException) e).getCode());
			} else {
				ret.setResultCode(ObjResultCode.ERROR);
			}
		}
		return ret;
	}
	
	/**
	 * 删除
	 * 
	 * @param record
	 * @return
	 */
	@RequestMapping("/deleteSysModule.do")
	@ResponseBody
	public ObjResultException<String> moduleAjaxDelete(Integer id) {
		ObjResultException<String> ret = new ObjResultException<String>();
		try {
			int success = sysModuleService.deleteByPrimaryKey(id);

			if (success > 0) {
				
				sysModuleAuthorityMapper.deleteByMid(id); //删除关联
				
				ret.setResultCode(ObjResultCode.OK);
			} else {
				ret.setResultCode(ObjResultCode.MODULE_DELETE_ERROR);
			}
		} catch (Exception e) {
			if (e.getClass().equals(ObjException.class)) {
				ret.setResultCode(((ObjException) e).getCode());
			} else {
				ret.setResultCode(ObjResultCode.ERROR);
			}
		}
		return ret;
	}
	
	/**
	 * 模块列表
	 */
	@RequestMapping("/selectRoleMeunAuthListByPid.do")
	@ResponseBody
	public ObjResultJSON<List<SysRoleModuleDto>> sysModuleAjaxList(HttpServletRequest request,HttpServletResponse response, Model model,Integer id,Integer rid) {
		ObjResultException<String> ret = new ObjResultException<String>();
		ObjResultJSON<List<SysRoleModuleDto>> retjson = new  ObjResultJSON<List<SysRoleModuleDto>>();
		try {
			Sys_Module module = new Sys_Module();
			module.setPid(id);
			module.setRid(rid);
			List<SysRoleModuleDto> list = sysModuleService.selectRoleMeunAuthListByPid(module);
			
			if(list!=null&&list.size()>0){
				retjson.setData(list);
				retjson.setResultCode(ObjResultCode.OK);
			}else{
				retjson.setResultCode(ObjResultCode.DATA_ERROR);
			}
		} catch (Exception e) {
			e.printStackTrace();
			if (e.getClass().equals(ObjException.class)) {
				ret.setResultCode(((ObjException) e).getCode());
			} else {
				ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
			}
			model.addAttribute("ret", ret);
		}
		return retjson;
	}
	
	
	/**
	 * 模块列表
	 */
	@RequestMapping("/sysOptionModuleAjaxList.do")
	@ResponseBody
	public ObjResultJSON<ArrayList<Sys_Module>> sysOptionModuleAjaxList(HttpServletRequest request,HttpServletResponse response, Model model,Integer id) {
		
		ObjResultException<String> ret = new ObjResultException<String>();
		ObjResultJSON<ArrayList<Sys_Module>> retjson = new  ObjResultJSON<ArrayList<Sys_Module>>();
		try {
			Sys_Module module = new Sys_Module();
			module.setPid(id);
			ArrayList<Sys_Module> list = sysModuleService.selectByPid(module);
			
			if(list!=null&&list.size()>0){
				retjson.setData(list);
				retjson.setResultCode(ObjResultCode.OK);
			}else{
				retjson.setResultCode(ObjResultCode.DATA_ERROR);
			}
		} catch (Exception e) {
			if (e.getClass().equals(ObjException.class)) {
				ret.setResultCode(((ObjException) e).getCode());
			} else {
				ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
			}
			model.addAttribute("ret", ret);
		}
		return retjson;
	}
	
	@RequestMapping("/allotRoleAuthorityLayer.do")
	public String allotRoleAuthorityLayer(String rid, Model model) {
		model.addAttribute("rid", rid);
		return "/system/roleauthority/allotRoleAuthorityTree";
	}
	
	@RequestMapping("/moduleAuthorityTreeJson.do")
	@ResponseBody
	public List<MeunAuthorityTreeJsonDto> moduleAuthorityTreeJson(Sys_Module module) {
		try {
			List<MeunAuthorityTreeJsonDto> list = sysModuleService.selectMeunAuthTree(module);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ArrayList<MeunAuthorityTreeJsonDto>();
	}
		
}
